// ========================================================================
// $Id: IO.java,v 1.13 2005/08/13 00:01:28 gregwilkins Exp $
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================

package org.mortbay.util;

import java.io.*;

public class IO
{

   public static int bufferSize = Integer.getInteger("org.mortbay.util.IO.bufferSize", 8192).intValue();

   public static void copy(InputStream in, OutputStream out) throws IOException
   {
      copy(in,out,-1);
   }

   public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException
   {
      byte buffer[] = new byte[bufferSize];
      int len=bufferSize;

      if (byteCount>=0)
      {
         while (byteCount>0)
         {
            if (byteCount<bufferSize)
               len=in.read(buffer,0,(int)byteCount);
            else
               len=in.read(buffer,0,bufferSize);

            if (len==-1)
               break;

            byteCount -= len;
            out.write(buffer,0,len);
         }
      }
      else
      {
         while (true)
         {
            len=in.read(buffer,0,bufferSize);
            if (len<0 )
               break;
            out.write(buffer,0,len);
         }
      }
   }

}
